package com.owbtgad.common.base

import android.app.ProgressDialog
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.owbtgad.common.MyProgressDialog
import com.owbtgad.common.R

/**
 * 创建日期 2020/9/24
 * 描述：
 */
abstract class BaseActivity : AppCompatActivity() {
    val TAG: String = this.javaClass.simpleName
    protected var mContext: Context? = null
    private var mProgressDialog: ProgressDialog? = null
    private var mToolbar: Toolbar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initContentView()
        mContext = this
        initView(savedInstanceState)
        /*ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .init()*/
    }

    protected open fun initContentView() {
        setContentView(getContentLayout())
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    protected abstract fun getContentLayout(): Int

    protected abstract fun initView(savedInstanceState: Bundle?)

    open fun setToolBar(toolbar: Toolbar?) {
        mToolbar = toolbar
        setSupportActionBar(mToolbar)
        //设置actionBar的标题是否显示，对应ActionBar.DISPLAY_SHOW_TITLE。
        supportActionBar!!.setDisplayShowTitleEnabled(false)
        setBackArrow()
    }

    protected open fun setTitleString(titles: String?) {
        if (mToolbar != null) {
            mToolbar!!.title = titles
        }
    }

    protected open fun setTitleString(title: CharSequence?) {
        if (mToolbar != null) {
            mToolbar!!.title = title
        }
    }

    override fun finish() {
        closeKeyboard()
        super.finish()
    }

    open fun closeKeyboard() {
        val view = window.peekDecorView()
        if (view != null) {
            val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

    override fun getResources(): Resources {
        val res = super.getResources()
        val config = res.configuration
        config.fontScale = 1.0f //1 设置正常字体大小的倍数
        res.updateConfiguration(config, res.displayMetrics)
        return res
    }

    protected open fun showProgressDialog() {
        showProgressDialog("")
    }

    protected open fun showProgressDialog(msg: String?) {
        if (mProgressDialog == null) {
            getProgressDialog()
        }
        if (!mProgressDialog!!.isShowing) {
            mProgressDialog!!.show()
        }
    }

    protected open fun dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog!!.isShowing) {
            mProgressDialog!!.dismiss()
        }
    }

    open fun getProgressDialog(): ProgressDialog? {
        return if (mProgressDialog == null) createDialog() else mProgressDialog
    }

    private  fun createDialog(): ProgressDialog? {
        mProgressDialog = MyProgressDialog(this)
        mProgressDialog?.setCanceledOnTouchOutside(false)
        return mProgressDialog
    }

    /**
     * 设置左上角back按钮
     */
    open fun setBackArrow() {
        setBackArrow(R.drawable.ic_baseline_arrow_back_24)
    }

    /**
     * 设置左上角back按钮
     */
    open fun setBackArrow(drawable: Int) {
        val upArrow = resources.getDrawable(drawable)
        //给ToolBar设置左侧的图标
        supportActionBar!!.setHomeAsUpIndicator(upArrow)
        // 给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        //设置返回按钮的点击事件
        setNavigationOnClickListener()
    }

    protected open fun setNavigationOnClickListener() {
        if (mToolbar == null) {
            return
        }
        mToolbar!!.setNavigationOnClickListener { MyLeftClick(false) }
    }

    protected open fun MyLeftClick(b: Boolean) {
        finish()
    }

    protected open fun onNoNetworkClick() {}


}